package com.example.demo.common;

import java.util.LinkedHashSet;
import java.util.Set;

public class JavaProperties {

    // 包名
    private final String pkg;
    // 类名
    private final String entityName;

    // 属性集合  需要改写 equals hash 保证名字可不重复 类型可重复
    private final Set<Field> fields = new LinkedHashSet<>();
    // 导入类的不重复集合
    private final Set<String> imports = new LinkedHashSet<>();

    public JavaProperties(String entityName, String pkg) {
        this.entityName = entityName;
        this.pkg = pkg;
    }

    public void addField(Class<?> type, String fieldName) {
        // 处理 java.lang
        final String pattern = "java.lang";
        String fieldType = type.getName();
        if (!fieldType.startsWith(pattern)) {
            // 处理导包
            imports.add(fieldType);
        }
        Field field = new Field();
        // 处理成员属性的格式
        int i = fieldType.lastIndexOf(".");
        field.setFieldType(fieldType.substring(i + 1));
        field.setFieldName(fieldName);
        fields.add(field);
    }

    public String getPkg() {
        return pkg;
    }

    public String getEntityName() {
        return entityName;
    }

    public Set<Field> getFields() {
        return fields;
    }

    public Set<String> getImports() {
        return imports;
    }
}
